﻿@model IEnumerable<GeoBox.Database.Device>
@{
    ViewBag.Title = "Index";
}

<h2>GPS-Clients</h2>

<table class="table">
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>UtcLastContact</th>
        <th>UtcLastGPS</th>
        <th>Status</th>
        <th>GPS-Signal</th>
    </tr>


</table>


@section scripts{
    <script>
        $(function () {
            getData();
        });


        function getData() {
            var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "Device" })';
            $.ajax({
                url: url,
                type: 'GET',
                success: function (result) {
                    $.each(result, function (index, item) {
                        $("table").find("tr:gt(0)").remove();

                        var onlineImg = '<img src="Content/Images/bullet-red.png" />';
                        if (item.IsOnline)
                            onlineImg = '<img src="Content/Images/bullet-green.png" />';

                        var gpsImg = '<img src="Content/Images/bullet-red.png" />';
                        if (item.HasGpsSignal)
                            gpsImg = '<img src="Content/Images/bullet-green.png" />';

                        $('table > tbody:last').append('<tr><td>' + item.Device.Id + '</td><td>' + item.Device.Name + '</td><td>' + item.Device.UtcLastContact + '</td><td>' + item.Device.LastLocation.UtcTime + '</td><td>' + onlineImg + '</td><td>' + gpsImg + '</td></tr>');
                    });
                }
            });

            setTimeout(getData, 5000);
        }
    </script>
}